package yygh.hosp.Service.imp;


import com.alibaba.fastjson.JSONObject;
import yygh.hospital.model.hosp.Department;
import yygh.hospital.vo.hosp.DepartmentQueryVo;
import yygh.hospital.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import yygh.hosp.Repository.DeparmentRepository;
import yygh.hosp.Service.DeparmentService;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class DeparmentServiceImp implements DeparmentService {

    @Autowired
    DeparmentRepository deparmentRepository;


    //Service层实现科室信息的保存
    @Override
    public void save(Map<String, Object> map) {
        //首先将传过来的map集合转化成字符串
        String string = JSONObject.toJSONString(map);
        //然后利用json工具类将字符串转换成Hospital对象
        Department department = JSONObject.parseObject(string, Department.class);

        //根据得到的deparment对象，判读数据库中是否已经存在该对象
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();

        Department Curdeparment = deparmentRepository.getDeparmentByHoscodeAndDepcode(hoscode, depcode);

        if (Curdeparment!=null){
            //当前deparment已经存在，对其信息进行更新
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            deparmentRepository.save(department);
        }
            //当前deparment不存在
        department.setCreateTime(new Date());
        department.setUpdateTime(new Date());
        department.setIsDeleted(0);
        deparmentRepository.save(department);
    }

    //Service层实现科室信息的分页查询
    @Override
    public Page<Department> findPageDeparment(int page, int limit, DepartmentQueryVo departmentQueryVo) {

        //创建Pageable对象，封装分页信息
        Pageable pageable = PageRequest.of(page-1, limit);

        //根据deparmentqueryVo获得deparment对象
        Department department=new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

        //mongodb中的模糊查询，创建Example对象，实现条件查询
        ExampleMatcher matcher=ExampleMatcher.matching().
                withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Example<Department> example = Example.of(department,matcher);

        //调用deparmentRepository的findAll（）方法
        Page<Department> departments = deparmentRepository.findAll(example, pageable);

        return departments;
    }

    //Service层实现科室信息的删除操作
    @Override
    public void remove(String hoscode, String depcode) {
        //根据传入的hoscode和depcode查找deparment
        Department deparment = deparmentRepository.getDeparmentByHoscodeAndDepcode(hoscode, depcode);

        //如果deparment存在，则进行删除
        if (deparment!=null){
            deparmentRepository.deleteById(deparment.getId());
        }
    }

    //Service层实现根据医院编号hoscode查询对应的所有科室的信息
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();

        //根据医院编号，查询医院所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);
        //所有科室列表 departmentList
        List<Department> departmentList = deparmentRepository.findAll(example);

        //根据大科室编号  bigcode 分组，获取每个大科室里面下级子科室
        Map<String, List<Department>> deparmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map集合 deparmentMap
        for(Map.Entry<String,List<Department>> entry : deparmentMap.entrySet()) {
            //大科室编号
            String bigcode = entry.getKey();
            //大科室编号对应的全局数据
            List<Department> deparment1List = entry.getValue();
            //封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(deparment1List.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for(Department department: deparment1List) {
                DepartmentVo departmentVo2 =  new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                //封装到list集合
                children.add(departmentVo2);
            }
            //把小科室list集合放到大科室children里面
            departmentVo1.setChildren(children);
            //放到最终result里面
            result.add(departmentVo1);
        }
        //返回
        return result;
    }

    @Override
    public String getDepName(String hoscode,String depcode ) {
        Department department=deparmentRepository.getDeparmentByHoscodeAndDepcode(hoscode,depcode);
        if (department!=null){
            return department.getDepname();
        }
        return  null;
    }

    //根据医院编号和科室编号获取对应的科室
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return  deparmentRepository.getDeparmentByHoscodeAndDepcode(hoscode,depcode);
    }
}
